package mkis.service.root.interceptor;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mkis.service.root.constants.Constants;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginIntercptor implements HandlerInterceptor {
	
	private Logger logger=LoggerFactory.getLogger(this.getClass());
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object hanlder) throws Exception {
		
		String url=request.getRequestURL().toString();
		String cp=request.getContextPath();
		
		if(url.indexOf(Constants.NO_CHK_URL)!=-1) {
			return true;
		} else {
			HttpSession session=request.getSession();
			
			Object sessionId=session.getAttribute(Constants.SESSION_MEMBER);
			
			if(sessionId==null){
				logger.info("========세션정보가 없어 로그인페이지로 리다이렉트.========");
				
				response.sendRedirect(cp+Constants.NOSESSION_URL);
				return false;
			} else {
				return true;
			}
		}
	}
	
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView mav) throws Exception {
		
	}
	
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)	throws Exception {
		
	}

}
